package com.gangquan360.smartadmin.module.supplier.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.supplier.dao.SupplierDao;
import com.gangquan360.smartadmin.module.supplier.domain.dto.SupplierDTO;
import com.gangquan360.smartadmin.module.supplier.domain.dto.SupplierQueryDTO;
import com.gangquan360.smartadmin.module.supplier.domain.entity.SupplierEntity;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 供应商表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-14 13:48:57
 * @since JDK1.8
 */
@Service
public class SupplierService {

    @Autowired
    private SupplierDao supplierDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-14 13:48:57
     */
    public ResponseDTO<PageResultDTO<SupplierDTO>> queryByPage(SupplierQueryDTO queryDTO) {
        Page page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<SupplierEntity> entities = supplierDao.queryByPage(page, queryDTO);
        List<SupplierDTO> dtoList = SmartBeanUtil.copyList(entities, SupplierDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<SupplierDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-14 13:48:57
     */
    public ResponseDTO<String> add(SupplierDTO addDTO) {
        SupplierEntity entity = SmartBeanUtil.copy(addDTO, SupplierEntity.class);
        supplierDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-14 13:48:57
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(SupplierDTO updateDTO) {
        SupplierEntity entity = SmartBeanUtil.copy(updateDTO, SupplierEntity.class);
        supplierDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-14 13:48:57
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        supplierDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-14 13:48:57
    */
    public ResponseDTO<SupplierDTO> detail(Long id){
        SupplierEntity entity = supplierDao.selectById(id);
        SupplierDTO dto =  SmartBeanUtil.copy(entity,SupplierDTO.class);
        return ResponseDTO.succData(dto);
    }
}
